﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SEO.Business;
using SEO.Entity;
using System.Data;

/// <summary>
/// 关键字操作
/// </summary>
public partial class DoKeywords : AdminPage
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitData();
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.KeywordsList;
    }

    protected void InitData()
    {
        DataTable dtSort = GetAllSort();
        ddlSort.Items.Clear();
        ddlSort.Items.Add(new ListItem("请选择分类","0"));
        foreach (DataRow drSort in dtSort.Rows)
        {
            ddlSort.Items.Add(new ListItem(drSort["Name"].ToString(),drSort["ID"].ToString()));
        }
        dtSort.Dispose();
    }


    /// <summary>
    /// 提交操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string[] nameList = txtName.Text.Trim().Split('\n');
        if (nameList.Length == 0)
        {
            Until.Alert("请输入关键字！");
            return;
        }
        BisKeywords bisInfo = new BisKeywords();
        List<string> keysList = new List<string>();
        for (int i = 0; i < nameList.Length; i++)
        {
            if (nameList[i].Length > 50)
            {
                Until.Alert("第" + (i + 1).ToString() + "行的关键字不能超过50个字符。");
                return;
            }
            if (!string.IsNullOrEmpty(nameList[i].Trim()))
            {
                if (bisInfo.CheckKeywords(0, nameList[i].Trim()))
                {
                    Until.Alert("第" + (i + 1).ToString() + "行的关键字已经存在。");
                    return;
                }
                keysList.Add(nameList[i].Trim());
            }
        }
        
        bool bResult = true;
        try
        {
            int nSortID = Convert.ToInt32(ddlSort.SelectedValue);
            double dScale = Convert.ToDouble(txtScale.Text.Trim());
            keysList = keysList.Distinct<string>().ToList();
            if (keysList.Count > 0)
            {
                foreach (string strKey in keysList)
                {
                    Keywords Info = new Keywords();
                    Info.SortID = nSortID;
                    Info.Scale = dScale;
                    Info.Name = strKey;
                    bisInfo.Insert(Info);
                }
            }
            bResult = true;
        }
        catch
        {
            bResult = false;
        }
        //结果处理
        if (bResult)
        {
            Until.AlertJS("提交成功！", "ResetPage();");
        }
        else
        {
            Until.Alert("提交失败！");
        }
        RemoveKeywords();
    }
}